<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class AddClockBalance extends \app\BaseModel
{
    protected $name = "massage_addclock_balance_config";
    public function getBalanceAttr($value)
    {
        if(!empty($value)) {
            return round($value, 2);
        }
    }
    public function initData($uniacid, $admin_id = 0)
    {
        $key = "addclock_balance_config";
        incCache($key, 1, $uniacid, 10);
        if(getCache($key, $uniacid) == 1) {
            $arr = [1, 2, 3, 4];
            foreach ($arr as $value) {
                $find = $this->where(["type" => $value, "uniacid" => $uniacid, "admin_id" => $admin_id])->find();
                if(empty($find)) {
                    $insert = ["uniacid" => $uniacid, "type" => $value, "admin_id" => $admin_id];
                    $this->insert($insert);
                }
            }
        }
        decCache($key, 1, $uniacid);
        return true;
    }
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateOne($id)
    {
        $user_id = $this->where(["id" => $id])->value("user_id");
        $res = $this->where(["user_id" => $user_id])->where("id", "<>", $id)->update(["status" => 0]);
        return $res;
    }
    public function getObjBalance($order, $balance, $type, $admin_id = 0)
    {
        if($order["is_add"] == 0) {
            return $balance;
        }
        $admin_model = new Admin();
        $auth = 1;
        if($type == 1 && !empty($admin_id)) {
            $auth = $admin_model->where(["id" => $admin_id, "agent_coach_auth" => 1])->value("reseller_auth");
        }
        if($type == 3 && !empty($admin_id)) {
            $auth = $admin_model->where(["id" => $admin_id, "agent_coach_auth" => 1])->value("salesman_auth");
        }
        if($type == 4 && !empty($admin_id)) {
            $auth = $admin_model->where(["id" => $admin_id, "agent_coach_auth" => 1])->value("channel_auth");
        }
        if($auth != 1 || $type == 2) {
            $admin_id = 0;
        }
        $dis = ["uniacid" => $order["uniacid"], "type" => $type, "admin_id" => $admin_id];
        $data = $this->dataInfo($dis);
        if(empty($data)) {
            return $balance;
        }
        if($data["status"] == 0) {
            return 0;
        }
        if($data["add"] == 1) {
            return 100 < $balance + $data["balance"] ? 100 : $balance + $data["balance"];
        }
        return $balance - $data["balance"] < 0 ? 0 : $balance - $data["balance"];
    }
}

?>